<template>
  <view>
    <view class="qdbox3">
      <view v-if="adConfig.wx_reward_signin" class="full-width-height">
        <image @click="navigateToAds" class="rounded-image" src="/pages_sub/static/qzjd.png" />
      </view>

      <siginIn :todaySign="todaySign" :sign_days="sign_days" @click="getSigin" />

      <view class="white-background">
        <view class="section-title">新手任务</view>
        <view v-for="task in newTasks" :key="task.id" class="admin_box">
          <view style="display: flex; justify-content: space-between; align-items: center;">
            <view>
              <view class="dou">
                <view class="task-title">{{ task.title }}</view>
                <image src="/pages_sub/static/jindou.jpg" class="jindou-image" />
                <view class="dou2">{{'+'+task.reward+'金豆' }}</view>
              </view>
              <view class="task-description">{{ task.description }}</view>
            </view>
          </view>
          <view v-if="userInfo.finish[task.type]" class="recharge completed">已完成</view>
          <view v-else class="recharge" @click="completeTask(task)">去完成</view>
        </view>
      </view>

      <view class="white-background">

        <view class="section-title">成长任务</view>
        <view v-for="task in growthTasks" :key="task.id" class="admin_box">
          <view style="display: flex; justify-content: space-between; align-items: center;">
            <view>
              <view class="dou">
                <view class="task-title">{{ task.title }}</view>
                <image src="/pages_sub/static/jindou.jpg" class="jindou-image" />
                <view class="dou2">{{ '+'+task.reward+'金豆' }}</view>
              </view>
              <view class="task-description">{{ task.description }}</view>
            </view>
          </view>
          <view v-if="userInfo.finish[task.type]" class="recharge completed">已完成</view>
          <view v-else class="recharge" @click="completeTask(task)">去完成</view>
        </view>
      </view>
    </view>
    <banner-ad :config="adConfig.wx_banner_signin" />
  </view>
</template>

<script>
import common_mixin from '@/libs/mixin/common_mixin.js';
import BannerAd from "@/components/banner-ad/banner-ad.vue";
import SiginIn from '@/components/fenxiao-sign/sigin-in.vue';
import {hideLoading, toast} from "../../utils/utils";
import {doSign, getMyUserInfo} from "../../api/api";
import store from "../../store";
import number from "../../uni_modules/tuniao-ui/libs/function/number";

export default {
  mixins: [common_mixin],
  components: {
    BannerAd,
    SiginIn,
  },
  data() {
    return {
      kefuurl: "",
      userInfo: {},
      all_get: 0,
      sessionKey: "",
      encryptedKey: "",
      sign_days: 0,
      todaySign: false,
      newTasks: [
        { id: '2', title: '解锁智能推荐', description: '获取你的智能信息，解锁智能推荐', reward: '50', type: 'jiesuo' },
        { id: '5', title: '新人签到', description: '每日签到领金币', reward: '30', type: 'xinren' },
        // { id: '3', title: '获取手机号', description: '完善用户信息', reward: '+50金豆', type: 'shoujihao' },
        // { id: '6', title: '邀请好友', description: '每邀请1个好友奖励100金豆', reward: '+100金豆', type: 'yaoqing' },
      ],
      growthTasks: [
        { id: '1', title: '首充大礼包', description: '充一次送两次金豆', reward: '50', type: 'libao' },
        // { id: '4', title: '订阅提醒', description: '订阅我新剧上新不迷路', reward: '+15金豆', type: 'dingyue' },
        { id: '3', title: '看广告领金豆', description: '限量赠送，先到先得', reward: '100', type: 'ad' },
      ],
    };
  },
  async onShow() {
    let data = await this.$api.getMyUserInfo();
    this.userInfo = data.userinfo;
    console.log(this.userInfo)
    if (!this.userInfo.finish) {
      this.userInfo.finish={}
    } else {
        this.userInfo.finish=JSON.parse(this.userInfo.finish)
    }
  },
  onLoad() {
    this.loadData();
  },
  methods: {
    navigateToAds() {
      uni.navigateTo({ url: '/pages_sub/signin/wxads' });
    },
    async completeTask(task) {
      if (task.type === 'libao' ||task.type === 'xinren') {
        await this.callTaskFunction(task);
      } else if (task.type === 'jiesuo') {
        this.jiesuo(task);
      } else if (task.type === 'ad'){
        this.navigateToAds(task);
      }
    },
    jiesuo(task) {
      if (this.userInfo.nickname) {
        this.callTaskFunction(task)
      } else {
        this.tn("/pages_sub/setUserinfo/setUserinfo")
      }
    },
    async callTaskFunction(task) {
      await this.$api.completeTask ({type:task.type,reward:task.reward})
      toast('任务已完成');
      this.updateUserInfo(task)
    },
    updateUserInfo(task) {
      if (!this.userInfo.finish) {
        this.userInfo.finish={};
      }
      this.userInfo.finish[task.type] = true;
      this.userInfo.balance += Number(task.reward);
      store.commit('$tStore', {
        name: 'vuex_user',
        value: this.userInfo
      });
    },
    subscribeToMessages(callback) {
      uni.requestSubscribeMessage({
        tmplIds: vk.vuex.get('$app.msglist'),
        success: () => callback(),
      });
    },
    async loadData() {
      const info = await this.$api.signInfo();
      this.sign_days = info.sign_days;
      this.todaySign = info.todaySign;
      hideLoading();
    },
    async getSigin() {
      console.log("getSignin")
      if (this.todaySign) {
        toast('今日已签到');
        return;
      }
      // await this.ding(true);
      let params = {
        signin_reward: 60, //未设置默认30
        sign_datas: this.sign_days //连续签到天数
      }
      await this.$api.doSign(params);
      this.todaySign = true;
      this.loadData();
    },
    async ding(backToIndex) {
      await this.subscribeToMessages(() => vk.callFunction({
        url: 'client/user/kh/dingyue',
        showLoading: false,
        data: {
          msglist: vk.vuex.get('$app.msglist'),
          platform: "weixin",
          tv_id: "no",
          tv_name: "no",
        },
      }));
      if (backToIndex) uni.navigateBack();
    },
    takeWxAd() {
      uni.navigateTo({ url: "/pages_sub/signin/wxads" });
    },
  },
};
</script>

<style lang="scss">
.qdbox3 {
  background-color: #f5f6fa;
  height: 100%;
  padding: 20px;
}
.full-width-height {
  width: 100%;
  height: 250rpx;
}
.rounded-image {
  width: 100%;
  height: 100%;
  border-radius: 30rpx;
}
.white-background {
  background-color: #ffffff;
}
.section-title {
  color: orange;
  font-size: 35rpx;
  font-weight: 600;
  height: 80rpx;
  line-height: 80rpx;
  border-bottom: 1px dashed orange;
  margin-top: 60rpx;
  margin-bottom: 20px;
  padding: 20px;
}
.admin_box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20rpx;
  padding: 20rpx;
  margin-top: -20rpx;
  background-color: #fff;
  border-radius: 0 0 20rpx 20rpx;
}
.recharge {
  color: #fff;
  background-color: #e74202;
  padding: 10rpx 20rpx;
  border-radius: 30rpx;
  text-align: center;
}
.completed {
  background-color: #bcbcbc;
}
.dou {
  display: flex;
  align-items: center;
}
.task-title {
  font-weight: 600;
  font-size: 15px;
}
.jindou-image {
  width: 30rpx;
  height: 30rpx;
  margin-left: 10px;
}
.dou2 {
  color: #ff5500;
  font-size: 26rpx;
  font-weight: 600;
}
.task-description {
  color: gray;
  font-size: 12px;
  margin-top: 15px;
}
</style>
